<%--
  Created by IntelliJ IDEA.
  User: lichengming
  Date: 2017/8/3
  Time: 上午7:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/resources.jsp" %>
<html>
<head>
    <title>商品</title>
    <script>
        $(function () {
            $('#begin_time,#end_time').datetimepicker({
                language : 'zh-CN',
                format : 'yyyy-MM-dd HH:mm:ss',
            });
        })
    </script>
</head>
<body>
<div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">查询条件</h3>
        </div>
        <div class="panel-body">
            <form class="form-inline ems_search_form">
                <div class="form-group">
                    <input type="text" class="form-control" id="nickName" name="nickName" placeholder="店主姓名">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" name="store_name_" placeholder="店铺名">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" name="phone" placeholder="手机号">
                </div>
                <div class="form-group">
                    <select class="form-control" name="status_">
                        <option value="">请选择审核状态</option>
                        <option value="audit_success">审核成功</option>
                        <option value="auditing">审核中</option>
                        <option value="audit_fail">审核失败</option>
                    </select>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="begin_time" name="begin_time" placeholder="开始时间">
                    <label>-</label>
                    <input type="text" class="form-control" id="end_time"  name="end_time" placeholder="结束时间">
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-info btn-search">查询</button>
                    <button type="button" class="btn btn-default btn-reset">清空</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="ems_table_content">
    <div class="panel panel-default">
        <div class="panel-heading">商品列表</div>
        <div id="table_tools">
            <div class="form-inline" role="form">
                <button type="button" id="merchant_audit" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                    审核
                </button>
                <button type="button" id="merchant_batch_audit" class="btn btn-outline btn-info">
                    <i class="glyphicon glyphicon-check" aria-hidden="true"></i>
                    批量审核
                </button>
                <button type="button" id="item_recommend" class="btn btn-outline btn-success">
                    <i class="glyphicon glyphicon-heart" aria-hidden="true"></i>
                    推荐商品
                </button>
                <button type="button" id="item_unrecommend" class="btn btn-outline btn-danger">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    取消推荐商品
                </button>
            </div>
        </div>
        <table id="merchantTable"
               data-url="/ems/mallitem/pagination">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-field="nick_name" >商家昵称</th>
                <th data-field="contact_">手机号</th>
                <th data-field="store_name_">店铺名</th>
                <th data-field="name_" data-formatter="nameFormat">商品主标题</th>
                <th data-field="sub_name_">商品副标题</th>
                <th data-field="recommend" data-formatter="recommendFormat">推荐状态</th>
                <th data-field="create_time_">申请时间</th>
                <th data-field="status_" data-formatter="statusFormat">状态</th>
            </tr>
            </thead>
        </table>
    </div>
    <script>

        function nameFormat(value, row, index) {
            if(row.status_ != 'auditing'){
                return "<a href='javascript:;' onclick='tip()'>"+value+"</a>";
            }
            return "<a href='/ems/mallitem/audit?id="+row.id_+"'>"+value+"</a>";
        }

        function recommendFormat(srcValue) {
            if (srcValue == "N") {
                return "<span style='color: red;font-weight: bold' >未推荐</span>";
            } else {
                return "<span style='color: #3cb371;font-weight: bold'>已推荐</span>";
            }
        }

        function statusFormat(srcValue) {

            debugger;

            if (srcValue == "audit_success") {
                return "<span style='color: #3cb371;font-weight: bold' >审核成功</span>";
            }else if(srcValue == "auditing"){
                return "<span style='color: red;font-weight: bold'>审核中</span>";
            }else {
                return "<span style='color: red;font-weight: bold'>审核失败</span>";
            }
        }
    </script>
</div>
<script>
    $(function () {

        $("#merchantTable").createTable();

        //编辑
        $("#merchant_audit").on("click",function () {
            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要审核的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据审核");
                return;
            }

            var selectRow = selectRows[0];
            if(selectRow.status_ != 'auditing'){
                $.errorAlert("只能审核待审核记录");
                return;
            }
            window.location.href = "/ems/mallitem/audit?id=" + selectRow.id_;
        })

        // 批量审核
        $("#merchant_batch_audit").on("click",function () {
            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要审核的数据");
                return;
            }

            // 遍历勾选的记录组，若有记录是待审核，则提示。
            var selectRowItem = "";
            var selectRowList = "";
            for(var index in selectRows){
                selectRowItem = selectRows[index];
                if(selectRowItem.status_ != 'auditing'){
                    $.errorAlert("只能审核待审核记录");
                    return;
                }
                selectRowList += selectRowItem.id_ + ",";
            }
            selectRowList = selectRowList.substring(0,selectRowList.length - 1);
            $.confirmAlert("确定要审核这些商品吗",function(){
                $.emsAjax({
                    data:{selectRowList:selectRowList},
                    type:"POST",
                    url:"/ems/mallitem/auditBatchUpdate",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#merchantTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        })

        $("#item_recommend").on("click",function(){

            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要推荐的商家");
                return;
            }

            var shopIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                if(selectRows[i].status_ != 'audit_success'){
                    $.errorAlert("推荐的商品必须已审核");
                    return;
                }

                if(selectRows[i].recommend == 'Y'){
                    $.errorAlert("不能重复推荐");
                    return;
                }
                shopIdList += selectRows[i].id_ + ",";

            }
            shopIdList = shopIdList.substring(0,shopIdList.length - 1);
            $.confirmAlert("确定要推荐这些商品吗",function(){
                $.emsAjax({
                    data:{shopIdList:shopIdList},
                    type:"POST",
                    url:"/ems/mallitem/recommend",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#merchantTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        })

        //取消推荐
        $("#item_unrecommend").on("click",function(){

            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要取消推荐的商品");
                return;
            }

            var shopIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                if(selectRows[i].recommend == 'N'){
                    $.errorAlert("不能重复取消推荐");
                    return;
                }
                shopIdList += selectRows[i].id_ + ",";

            }
            shopIdList = shopIdList.substring(0,shopIdList.length - 1);
            $.confirmAlert("确定要取消推荐这些商家吗",function(){
                $.emsAjax({
                    data:{shopIdList:shopIdList},
                    type:"POST",
                    url:"/ems/mallitem/unrecommend",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#merchantTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        })
    });

    function tip(){
        $.errorAlert("只能审核待审核记录");
    }
</script>
</body>
</html>
